#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>

#include <QMenuBar>
#include <QMenu>
#include <QFileDialog>
#include <QStatusBar>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("记事本");
    //创建菜单栏
    QMenuBar* menuBar=this->menuBar();
    this->setMenuBar(menuBar);
    //创建菜单
    QMenu* fileMenu=new QMenu("文件");
    menuBar->addMenu(fileMenu);
    QMenu* exitMenu=new QMenu("退出");
    menuBar->addMenu(exitMenu);
    //创建菜单项
    QAction* openAction=new QAction("打开文件");
    QAction* saveAction=new QAction("保存文件");
    QAction* closeAction=new QAction("关闭文件");
    QAction* attributeAction=new QAction("文件属性");
    QAction* exitAction=new QAction("退出程序");
    fileMenu->addAction(openAction);
    fileMenu->addAction(saveAction);
    fileMenu->addAction(closeAction);
    fileMenu->addAction(attributeAction);
    exitMenu->addAction(exitAction);
    //链接信号槽
    connect(openAction,&QAction::triggered,this,&MainWindow::openFile);
    connect(saveAction,&QAction::triggered,this,&MainWindow::saveFile);
    connect(closeAction,&QAction::triggered,this,&MainWindow::closeFile);
    connect(attributeAction,&QAction::triggered,this,&MainWindow::attributeFile);
    connect(exitAction,&QAction::triggered,this,&MainWindow::programExit);
    //connect(exitAction, &QAction::triggered,[this](){this->close();});

    //指定一个对话框
    plainEdit=new QPlainTextEdit();
    QFont font;
    font.setPixelSize(20);
    plainEdit->setFont(font);
    this->setCentralWidget(plainEdit);


}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::openFile()
{
    //1.通过文件窗口选则路径
    QString pathName=QFileDialog::getOpenFileName(this);
    //2.把文件名添加到状态栏
    QStatusBar* statusBar=this->statusBar();
    statusBar->showMessage(pathName);
    //qDebug()<<pathName;
    //3.获取文件描述符
    QFile file(pathName);
    //4.打开文件
    int ret = file.open(QIODevice::ReadOnly);
    if(!ret)
    {
        statusBar->showMessage("打开文件失败");
        //qDebug()<<"打开文件失败";
        return;
    }
    //qDebug()<<"打开文件成功";
    //5.读取文件
    QString text = file.readAll();
    //qDebug()<<text;
    //plainEdit->setPlainText("打开文件成功");
    //6.关闭文件
    file.close();
    //7.显示文件内容
    plainEdit->setPlainText(text);
}

void MainWindow::saveFile()
{
    //1.通过文件窗口选则路径
    QString pathName=QFileDialog::getSaveFileName(this);
    //2.把文件名添加到状态栏
    QStatusBar* statusBar=this->statusBar();
    statusBar->showMessage(pathName);
    //3.获取文件描述符
    QFile file(pathName);
    //4.打开文件
    int ret = file.open(QIODevice::WriteOnly);
    if(!ret)
    {
        statusBar->showMessage("打开文件失败");
        //qDebug()<<"打开文件失败";
        return;
    }
    //qDebug()<<"打开文件成功";
    //5.获取文件
    QString text = plainEdit->toPlainText();
    //6.写入文件
    file.write(text.toUtf8());
    //qDebug()<<text;
    //plainEdit->setPlainText("打开文件成功");
    //7.关闭文件
    file.close();
}

void MainWindow::closeFile()
{
    //1.弹出保存框
    saveFile();
    //2.清除显示内容
    plainEdit->setPlainText("");
}

void MainWindow::attributeFile()
{
    // 弹出文件对话框, 并获取到文件的属性信息.
    QString pathName = QFileDialog::getOpenFileName(this);
    // 构造出一个 QFileInfo 对象
    QFileInfo fileInfo(pathName);
    // 打印相关属性
    QString text;
    text += fileInfo.fileName();
    text +="\n";
    text += fileInfo.suffix();
    text +="\n";
    text += fileInfo.path();
    text +="\n";
    text += fileInfo.size();
    text +="\n";
    text += fileInfo.isFile();
    text +="\n";
    text += fileInfo.isDir();
    text +="\n";

    // ......

    //显示文件属性
    plainEdit->setPlainText(text);
}

void MainWindow::programExit()
{
    this->close();
}

